<?php

namespace service;

use app\common\model\Books;
use think\Exception;
use think\facade\Cache;
use think\facade\Config;

class BookService
{
    private static $host = 'http://www.datangzww.com';
    private static $key = 'sMHeIsOqyS7xjVvycAyKReTQc';
    private static $success_code = 8200;
    private static $msg = [
        8400 => '请求参数错误',
        8401 => '身份验证错误',
        8403 => '拒绝请求',
        8404 => '书籍授权/请求书籍不存在',
        8405 => '书籍信息/章节错误'
    ];

    /**
     * 分类
     * @return bool|string
     */
    public static function category()
    {
        $url = self::$host.'/api/empower_khj/siteGetBkCategory.php';
        return HttpService::get($url, ['key' => self::$key]);
    }

    /**
     * 列表
     * @return bool|string
     */
    public static function books($c_id = 0)
    {
        $url = self::$host.'/api/empower_khj/siteGetBkList.php';
        $data = HttpService::get($url, ['key' => self::$key, 'c_id' => $c_id]);
        return self::handleData($data);
    }

    /**
     * 详情
     * @return bool|string
     */
    public static function info($bookid)
    {
        $url = self::$host.'/api/empower_khj/siteGetBkInfo.php';
        $data = HttpService::get($url, ['key' => self::$key, 'bookid' => $bookid]);
        return self::handleData($data);
    }

    /**
     * 章节
     *
     * @param $bookid
     *
     * @return bool|string
     */
    public static function chapter($bookid)
    {
        $url = self::$host.'/api/empower_khj/siteGetChList.php';

        $data = HttpService::get($url, ['key' => self::$key, 'bookid' => $bookid]);
        $result = self::handleData($data);
        if($result === false) {
            return false;
        }
        if(!Cache::store('book_redis')->has(Config::get('book.book_chapter_prefix').$bookid)) {
            if(is_array($result) && !empty($result)) {
                Cache::store('book_redis')->set(Config::get('book.book_chapter_prefix').$bookid, json_encode($result), 60 * 60 * 6);
            }
        }
        return json_decode(Cache::store('book_redis')->get(Config::get('book.book_chapter_prefix').$bookid), true);
    }

    /**
     * 章节内容
     *
     * @param $bookid
     * @param $chapterid
     *
     * @return bool|string
     */
    public static function content($bookid, $chapterid)
    {
        $url = self::$host.'/api/empower_khj/siteGetChCon.php';
        $data = HttpService::get($url, ['key' => self::$key, 'bookid' => $bookid, 'chapterid' => $chapterid]);
        return self::handleData($data);
    }

    private static function handleData($data)
    {
        $data = json_decode($data, true);
        if(isset($data['code']) && $data['code'] == self::$success_code) {
            return $data['result'] ?: [];
        }else{
            return false;
        }
    }

    //从第三方拉去图书并存储到本地数据库
    public static function capture()
    {
        $bookids=array_unique(array_column(self::books(),'bookid'));
        $bookids_local=Books::column('bookid');
        $bookids_diff=array_diff($bookids,$bookids_local);
        $books=[];
        $books_temp=[];
        foreach ($bookids_diff as $v){
            $books_temp[]=self::info($v);
        }
        foreach ($books_temp as $v){
            $v['author']=$v['authorname'];
            $v['update_status']=$v['fullflag'];
            $books[]=$v;
        }
        if ($books){
            (new Books())->saveAll($books);
        }
    }

    //更新本地图书的连载状态
    public static function update()
    {

    }
}